import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import {Movie} from './movie';
import {HttpClient} from "@angular/common/http";
import {URL} from '../config'
@Injectable({
  providedIn: 'root'
})
export class MoviesService {
  // getMovies():Observable<Movie[]>{
  //   return of(Movies);
  // }
  url=URL;

  //获取电影类型
  getTypes():Observable<any>{
    return this.http.get(this.url + 'movieTypes', {
      withCredentials: true
    });
  }
  //保存用户偏好
  saveTypes(mytypes:Set<string>,username:string):Observable<any>{
    let fd=new FormData()
    mytypes.forEach(i=>{
        console.log(i);
        fd.append('types',i);
      }
    )
    fd.append('username',username);
    return this.http.post(this.url+'interestToTypes',fd,{
      withCredentials: true
    })
  }
  //获取热门推荐电影
  getHotMovies():Observable<any>{
    return this.http.get(this.url+'recommendMoviesByMonth',{
      withCredentials: true
    })
  }
  //搜索电影
  getSearchMovies(keyword:string):Observable<any>{
    return this.http.get(this.url+'searchRecommend',{
      params:{
        keyword:keyword
      },
      withCredentials: true
    })
  }
  //根据id获取电影详细信息
  getMovieDetailById(id:number, username: string):Observable<any>{
    return this.http.get(this.url+'movieInfo',{
      params:{
        mid:id.toString(),
        username:username
      },
      withCredentials: true
    })
  }
  //为电影添加标签
  addTagForMovie(mid:number,username:string,tagName:string):Observable<any>{
    return this.http.get(this.url+'tag',{
      params:{
        mid:mid.toString(),
        username:username,
        tagName:tagName,
      },
      withCredentials: true
    })
  }
  //获取用户对电影的标签
  getUserTags(mid:number,username:string):Observable<any>{
    return this.http.get(this.url+'tagsOfUser',{
      params:{
        mid:mid.toString(),
        username:username
      },
      withCredentials: true
    })
  }
  //获取大家对电影的标签
  getAllTags(mid:number,username:string):Observable<any>{
    return this.http.get(this.url+'tagsOfOthers',{
      params:{
        mid:mid.toString(),
        username:username
      },
      withCredentials: true
    })
  }
  //根据电影id获取相似推荐
  getSimilarMoviesById(mid:number):Observable<any>{
    return this.http.get(this.url+'movieRecommendByMid',{
      params:{
        mid:mid.toString()
      },
      withCredentials: true
    })
  }
  //获取用户信息
  getUserInfo(username:string):Observable<any>{
    return this.http.get(this.url+'userInfo',{
      params:{
        username:username
      },
      withCredentials: true
    })
  }
  //根据电影类型做推荐
  getSimilarMoviesByTypes( movieType:string):Observable<any>{
    return this.http.get(this.url+'recommendMoviesByClass',{
      params:{
        movieType:movieType,
      },
      withCredentials: true
    })
  }
  //获取用户评分电影
  getUserScoredMovies(username:string):Observable<any>{
    return this.http.get(this.url+'moviesBeScoredByUser',{
      params:{
        username:username
      },
      withCredentials: true
    })
  }
  //为电影评分
  scoreMovie(username:string,mid:number,score:number):Observable<any>{
    return this.http.get(this.url+'scoreMovie',{
      params:{
        username:username,
        mid:mid.toString(),
        score:score.toString()
      },
      withCredentials: true
    })
  }
  //获取实时推荐电影
  getOnLineMovies(username:string):Observable<any>{
    return this.http.get(this.url+'recentlyRecommends',{
      params:{
        username:username
      },
      withCredentials: true
    })
  }
  constructor( private http: HttpClient) { }
}
